﻿using CefSharp;
using CefSharp.WinForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InfonavaEye
{

    //https://blog.csdn.net/cwr888/article/details/121978656

    public partial class BroswerForm : Form
    {
        /// <summary>
        /// Chromium浏览器实例
        /// </summary>
        public ChromiumWebBrowser WebBrowser;

        public BroswerForm()
        {
            InitializeComponent();
        }

        private void BroswerForm_Load(object sender, EventArgs e)
        {
            ///设置
            var setting = new CefSettings();
            setting.Locale = "zh-CN";// 设置语言
            setting.AcceptLanguageList = "zh-CN";
            setting.MultiThreadedMessageLoop = true;
            //setting.CachePath = "CHBrowser/BrowserCache";//缓存路径
            setting.AcceptLanguageList = "zh-CN,zh;q=0.8";//浏览器引擎的语言
            //setting.LocalesDirPath = "CHBrowser/localeDir";//日志
            //setting.LogFile = "CHBrowser/LogData";//日志文件
            //setting.PersistSessionCookies = true;//
            setting.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36";//浏览器内核
            //setting.UserDataPath = "CHBrowser/userData";//个人数据
            setting.CefCommandLineArgs.Add("disable-gpu", "1"); // 禁用GPU,解决闪烁.否则电脑上运行网页显示图形比例会不正常
            ///初始化
            CefSharp.Cef.Initialize(setting);
            WebBrowser = new ChromiumWebBrowser("https://baidu.com"); //初始页面

            #region //Bak
            //Old method
            //browser.RegisterJsObject("bound", new BoundObject(), options: BindingOptions.DefaultBinder);
            //Replaced with
            //CefSharpSettings.WcfEnabled = true;
            //browser.JavascriptObjectRepository.Settings.LegacyBindingEnabled = true;
            //browser.JavascriptObjectRepository.Register("bound", new BoundObject(), isAsync: false, options: BindingOptions.DefaultBinder);

            //Old Method
            //browser.RegisterAsyncJsObject("boundAsync", new AsyncBoundObject(), options: BindingOptions.DefaultBinder);
            //Replaced with
            //browser.JavascriptObjectRepository.Settings.LegacyBindingEnabled = true;
            //browser.JavascriptObjectRepository.Register("boundAsync", new AsyncBoundObject(), isAsync: true, options: BindingOptions.DefaultBinder);
            #endregion

            //Font font = new Font("hiragino sans gb", 7f);
            Font font = new Font("wenquanyi micro hei", 7f);
            //Font font = new Font("sans - serif", 7f);
            //Font font = new Font(".PingFang SC Medium", 7f);
            WebBrowser.Font = font;
            WebBrowser.Dock = DockStyle.Fill;//铺满                                                                  
            WebBrowser.Dock = DockStyle.Fill;//设置停靠方式
            WebBrowser.JavascriptObjectRepository.Settings.LegacyBindingEnabled = true; //交互数据
            //CefSharpSettings.WcfEnabled = true;
            //（bound为前端被调用的对象名称.如：bound.login();BoundObject为C#被暴露的Class对象，对应的js调用的方法就是BoundObject.Login()）
            //WebBrowser.JavascriptObjectRepository.Register("bound", new BoundObject(), isAsync: false, options: BindingOptions.DefaultBinder); //交互数据
            //WebBrowser.JavascriptObjectRepository.Register("boundAsync", new AsyncBoundObject(), isAsync: true, options: BindingOptions.DefaultBinder);
            this.panel1.Controls.Add(WebBrowser);//加入窗体
        }

        private void BroswerForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Cef.Shutdown();
        }

        public class BoundObject
        {
            public string MessageText { get; set; }

            public void Getvalue()
            {
                MessageBox.Show("收到JS调用,我是NET弹框.\n\r" + MessageText);
                //return "aaaaaaa";
            }

            public void GetValueArg(string inss)
            {
                MessageBox.Show("收到JS带参数调用,我是NET弹框.\n\r" + inss);
            }
        }
    }
}
